import { setProfile } from "@/store/modules/user";
import { useAxiosWithAuth } from "@/utils/useAxiosWidthAuth";
import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";

// 登录api
export const useLoginFn = function (props: {
  account: string;
  password: string;
}) {
  const axios = useAxiosWithAuth();
  const dispatch = useDispatch();

  useEffect(() => {
    async function login() {
      const res: any = await axios({
        method: "post",
        url: "login",
        data: props,
      });
      console.log(res);

      dispatch(setProfile(res.result));
    }
    login();
  }, [dispatch]);
};

export const useUserQQLogin = function (unionId: number | string, source = 6) {
  const axios = useAxiosWithAuth();
  const [result, setResult] = useState<any>();
  useEffect(() => {
    async function findHotGoods() {
      const res: any = await axios({
        method: "post",
        url: "/login/social",
        params: { unionId, source },
      });
      setResult(res.result);
    }
    findHotGoods();
  }, [unionId, source]);
  return { result };
};
